package com.renjibo.teawithmilkgoods.fragments;


import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.renjibo.lazyviewpager.LazyFragmentPagerAdapter;
import com.renjibo.lazyviewpager.LazyViewPager;
import com.renjibo.pagerslidingtabstrip.PagerSlidingTabStrip;
import com.renjibo.teawithmilkgoods.R;
import com.renjibo.teawithmilkgoods.views.NewsFragmentTabHost;

import java.util.ArrayList;
import java.util.List;

/**
 * 优惠界面
 * A simple {@link Fragment} subclass.
 * Use the {@link TopicFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TopicFragment extends Fragment implements LazyFragmentPagerAdapter.Laziable{
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String urlTitle="http://eleteamapi.ygcr8.com/v1";
    private String mParam1;
    private String mParam2;
    private PagerSlidingTabStrip topic_pagerSlidingTabStrip;
    private LazyViewPager topic_lazyViewPager;
    private List<String> topicTitle;
    private String urlString[]={"/product/list-featured-price","/product/list-featured-topic","/product/list-top-seller"};

    public TopicFragment() {
        // Required empty public constructor
    }


    public static TopicFragment newInstance(String param1, String param2) {
        TopicFragment fragment = new TopicFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        topicTitle=new ArrayList<>();
        topicTitle.add("天天优惠");
        topicTitle.add("为你精选");
        topicTitle.add("亲的最爱");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_topic, container, false);
        initView(view);
        return view;
    }
    public void initView(View view){
        topic_pagerSlidingTabStrip = (PagerSlidingTabStrip) view.findViewById(R.id.topic_pagerSlidingTabStrip);
        topic_lazyViewPager = (LazyViewPager) view.findViewById(R.id.topic_lazyViewPager);
        topic_lazyViewPager.setAdapter(new TopicFragmentPagerAdapter(getFragmentManager()));
        //平分屏幕
        topic_pagerSlidingTabStrip.setShouldExpand(true);
        topic_pagerSlidingTabStrip.setViewPager(topic_lazyViewPager);
    }
    class TopicFragmentPagerAdapter extends LazyFragmentPagerAdapter{

        public TopicFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        protected Fragment getItem(ViewGroup container, int position) {
            return GoodsDetailsFragment.newInstance("",urlTitle+urlString[position]+"");
        }

        @Override
        public int getCount() {
            return topicTitle.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return topicTitle.get(position);
        }
    }
    /**
     * dp转为px
     * @param context  上下文
     * @param dipValue dp值
     * @return
     */
    private int dip2px(Context context, float dipValue)
    {
        Resources r = context.getResources();
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dipValue, r.getDisplayMetrics());
    }

}
